//package com.sztz.service.impl;
//
//import cn.hutool.core.util.StrUtil;
//import com.sztz.constants.CommonConstants;
//import com.sztz.entity.SecurityUser;
//import com.sztz.entity.User;
//import com.sztz.service.UserService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.core.authority.AuthorityUtils;
//import org.springframework.security.core.userdetails.UserDetails;
//import org.springframework.security.core.userdetails.UserDetailsService;
//import org.springframework.stereotype.Service;
//
///**
// * 实现UserDetailsService接口，从数据库获取用户信息
// * @author yzy
// * @date 2023/3/22 10:28
// */
//@Service
//public class MyUserDetailServiceImpl implements UserDetailsService {
//    private final UserService userService;
//
//    @Autowired
//    public MyUserDetailServiceImpl(UserService userService) {
//        this.userService = userService;
//    }
//
//    /**
//     * 获取用户信息
//     * @param username 登录账号
//     * @return UserDetails
//     * @author yzy
//     * @date 2023/3/22 14:58
//     */
//    @Override
//    public UserDetails loadUserByUsername(String username) {
//        User user = userService.loadUserByUsername(username);
//        //将User数据库实体对象转换成SpringSecurity用户对象
//        SecurityUser securityUser = new SecurityUser(user.getUserCode(), user.getPassword());
//        //securityUser.setStatus(user.getStatus());
//        //组装用户权限,需要在前面加上ROLE_的前缀，这是SpringSecurity的默认要求
//        String[] roleArr = user.getRoles().stream().map(role -> StrUtil.addPrefixIfNot(role.getRoleCode(), CommonConstants.ROLE_PREFIX)).distinct().toArray(String[]::new);
//        //权限设置到UserDetails
//        securityUser.setAuthorities(AuthorityUtils.createAuthorityList(roleArr));
//        return securityUser;
//    }
//}
